
package progentiere.knapsackprb;


public class DPCoreLabel
{

    private int a;
    private int b;
    private double wBar;
    private double wTilde;
    private double pBar;
    private double pTilde;
    private Solution x;
    
    public DPCoreLabel(int a, int b, double wBar, double pBar, Solution x) 
    {
        this.a = a;
        this.b = b;
        this.wBar = wBar;
        this.pBar = pBar;
        this.x = x;
        this.wTilde = 0;
        this.pTilde = 0;
    }
    
    public DPCoreLabel(int a, int b, double wBar, double wTilde, double pBar, double pTilde, Solution x) 
    {
        this(a, b, wBar, pBar, x);
        this.wTilde = wTilde;
        this.pTilde = pTilde;
    }

    public DPCoreLabel(DPCoreLabel toCopy)
    {
        this(toCopy.a, toCopy.b, toCopy.wBar, toCopy.wTilde, toCopy.pBar, toCopy.pTilde, toCopy.x);
    }
    
    public int getA() {
        return a;
    }

    public int getB() {
        return b;
    }

    public double getwBar() {
        return wBar;
    }

    public double getwTilde() {
        return wTilde;
    }

    public double getpBar() {
        return pBar;
    }

    public double getpTilde() {
        return pTilde;
    }
    
    public double getP() {
        return pBar + pTilde;
    }

    public double getW() {
        return wBar + wTilde;
    }

    public Solution getX() {
        return x;
    }
    
    @Override
    public String toString() 
    {
        return "DPLabel{" + "a=" + a + ", b=" + b + ", w=" + this.getW() + ", p=" + this.getP() + '}';
    }

    public boolean dominates(DPCoreLabel label) 
    {
        if(this.getW() < label.getW() && this.getP() >= label.getP())
        {
            return true;
        }
        if(this.getW() == label.getW() && this.getP() > label.getP())
        {
            return true;
        }
        
        return false;
    }
    
    // is the value of the relaxed solution better thatn the incumbent ?
    public boolean isRelaxLesserThanInc(KnapSackProblem problem, double incumbent)
    {
        double bound = this.pBar;
        double remainCap = problem.getCapacity() - this.wBar;
        /*
        for(int i = 0 ; i < this.a ; i++)
        {
            if(problem.getItems().get(i).getWeight() <= problem.getCapacity() - remainCap)
            {
                bound += problem.getItems().get(i).getProfit();
                remainCap -= problem.getItems().get(i).getWeight();
            }
        }
        */
        for(int i = b + 1 ; i < problem.getNbItems() ; i++)
        {
           if(problem.getItems().get(i).getWeight() <= problem.getCapacity() - remainCap)
            {
                bound += problem.getItems().get(i).getProfit();
                remainCap -= problem.getItems().get(i).getWeight();
            }
        }
        
        return bound <= incumbent;
    }
}
